package com.demo.java.disruptor.netty.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

// 消息编码
public class MessageEncode extends MessageToByteEncoder<TranslatorData> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, TranslatorData translatorData, ByteBuf byteBuf) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(translatorData);
        byte[] objBytes = baos.toByteArray();
        byteBuf.writeBytes(objBytes);
        oos.close();
    }
}
